<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Salesman;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Str;

class SalesmanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Salesman(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('type')->display(function ($val) {
                return \App\Models\Salesman::TYPE_MAP[$val];
            });
            $grid->column('contact');
            $grid->column('url');
            //$grid->column('users_count', '分配客户');
            $grid->column('forbid', '状态')->display(function ($value) {
                return \App\Models\Salesman::FORBID_MAP[$value];
            });
            //$grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->showQuickEditButton();
            $grid->quickSearch('contact')->placeholder(trans('salesman.fields.contact'));

            $grid->disableDeleteButton();
            $grid->disableRowSelector();
            $grid->disableFilterButton();
            $grid->disableViewButton();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Salesman(), function (Form $form) {
            $form->display('id');

            $form->select('type')->options(\App\Models\Salesman::TYPE_MAP)->required();
            $form->text('contact')->required();
            $form->text('url')->required();
            $form->switch('forbid');
            $form->saving(function (Form $form) {
                if (Str::contains($form->input('contact'), '/')) {
                    return $form->response()->error('业务号不能包含/');
                }
            });
        });
    }
}
